#!/usr/bin/awk -f 
BEGIN{
    if(!Outfile) {
        print "usage: awk -f statis_guid_tsv.awk -v Outfile=value file ...";
        exit;
    }

    Time_begin = systime();
    print strftime("%Y-%m-%d %T",Time_begin) " Starting...";
}

{
    parse_query($2,result);

    # 合作ID
    pid = result["pid"];
    # 设备ID
    deviceid = result["deviceid"];
    # 平台
    os = result["os"];
    # 平台版本
    os_ver = result["ver"];
    # 设备型号
    btype = result["btype"];
    # 设备品牌
    brand = result["brand"];
    # 运营商
    operator = result["operator"];
    client_ver = result["version"];
    regtime = ios8601Totimestamp($1,reTime);
    lasttime = regtime;

    guid_diff = result["guid"];

    # 用户唯一标识 guid
    guid = result["mac"]"&"result["imei"]"&"deviceid"&"result["uuid"];
    ##sh_md5 = "echo -n '"result["mac"]"&"result["imei"]"&"deviceid"&"result["uuid"]"' | md5sum | awk '{print $1}'"; 
    ##sh_md5 = "perl -MDigest::MD5=md5_hex -e'print md5_hex(\""result["mac"]"&"result["imei"]"&"deviceid"&"result["uuid"]"\")'";
    #sh_md5 | getline guid;
    #close(sh_md5);

    if(guid_diff && guid_diff !~ /^[0-9a-z]{32}$/){
        #guid_diff = "";
        next;
    }

    # btype_decode
    btype_decode = "";
    if(tmp_btype[btype]) {
       btype_decode = tmp_btype[btype];
    }else{
       bt_decode = "perl -MURI::Escape -e'print uri_unescape(\""btype"\")'";
       bt_decode | getline btype_decode;
       tmp_btype[btype] = btype_decode;
       close(bt_decode);
    }

    # operator_decode 
    operator_decode = "";
    if(tmp_operator[operator]) {
        operator_decode = tmp_operator[operator];
    }else if(operator){
        url_decode = "perl -MURI::Escape -e'print uri_unescape(\""operator"\")'";
        url_decode | getline operator_decode;
        tmp_operator[operator] = operator_decode;
        close(url_decode);
    }

    # guid,pid,os,os_ver,btype,brand,operator,client_ver,deviceid,regtime,lasttime
    #if(NR % 1000 == 0) {
    #    tmp_line = tmp_line""guid","pid","os","os_ver","btype","brand","operator_decode","client_ver","deviceid","regtime","lasttime"";
    #    print tmp_line >> Outfile;
    #    tmp_line = "";
    #}else{
    #    tmp_line = tmp_line""guid","pid","os","os_ver","btype","brand","operator_decode","client_ver","deviceid","regtime","lasttime"\n";
    #}
    #print guid"||"pid"||"os"||"os_ver"||"btype_decode"||"brand"||"operator_decode"||"client_ver"||"deviceid"||"regtime"||"lasttime"||"guid_diff >> Outfile;
    print "\""guid"\""",""\""pid"\""",""\""os"\""",""\""os_ver"\""",""\""btype_decode"\""",""\""brand"\""",""\""operator_decode"\""",""\""client_ver"\""",""\""deviceid"\""",""\""regtime"\""",""\""lasttime"\""",""\""guid_diff"\"" >> Outfile;
}

END{
    if(Time_begin){
        Time_end = systime();
        print strftime("%Y-%m-%d %T",Time_end) " End";
        print "Take Time: " Time_end - Time_begin "s";
        print "Row Total: " NR "\n";
    }
}

# time_iso8601 to unix timestamp
function ios8601Totimestamp(timeStr,reTime) {
    gsub(/[\42T\-:]/," ",timeStr);
    split(timeStr,reTime,"+");
    return mktime(reTime[1]);
}

function parse_query(str,array) {
    delete array;
    split(str,arr,"&");
    for(i=1;i<=length(arr);i++) {
        split(arr[i],tmp,"=");
        if(length(tmp) > 2){
	        tmp_offset = index(arr[i],"=");
	        array[tmp[1]] = substr(arr[i],tmp_offset,length(arr[i]) - length(tmp[1]));
        }else{
            array[tmp[1]] = tmp[2];
        }
    }
}
